<?php
/**
 * (exporter)
 */
namespace App\Exporter\Delivery;

use App\Models\Basics\SysPlatAccount;
use App\Models\Basics\SysCountry;
use App\Models\Delivery\FBADelivery;
use App\Models\Product\ProductClassify;
use App\Util\User;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;
use Illuminate\Contracts\Queue\ShouldQueue;
class FBADetailExporter extends ExcelExporter implements WithMapping, ShouldQueue
{

    protected $fileName = 'FBA发货单明细.csv';

    protected $headings = ['发货单','发货账号','国家','销售员','SKU','产品名称','季节性','品类','状态','物流状态',
        '平台SKU','ASIN','FNSKU','数量','已确定数量','已到货数量','发货时间','首次到货时间','发货时效','备注'];

    public function map($model) : array
    {
        return [
            $model->delivery_no,
            SysPlatAccount::getAccountById($model->delivery->account_id ?? ''),
            SysCountry::getCountryById($model->delivery->country_id ?? ''),
            User::getUsernameById($model->delivery->seller_id ?? -1),
            $model->sku->psku_code ?? '',
            $model->sku->psku_cn ?? '',
            ProductClassify::$_season[$model->skuClassify->season ?? 3],
            $model->skuClassify->title ?? '',
            array_key_exists($model->delivery->status,FBADelivery::$_status) ? FBADelivery::$_status[$model->delivery->status] : '',
            array_key_exists($model->delivery->logistics_status,FBADelivery::$_logisticsStatus) ? FBADelivery::$_logisticsStatus[$model->delivery->logistics_status] : '',
            $model->seller_sku,
            $model->asin,
            $model->fnsku,
            $model->apply_num,
            $model->ship_num,
            $model->received_num,
            $model->delivery->delivered_at ?? '',
            $model->received_date,
            $model->received_date ? round((strtotime($model->received_date) - strtotime($model->delivery->delivered_at))/3600/24,2) : '',
            $model->delivery->remarks ?? '',
        ];
    }
}
